package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffWorkListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffWork;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包人员工作记录
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月20日
 */
@Controller
@Scope("prototype")
public class UecOutStaffWorkListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 外包人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecOutStaffWorkListboxBean bean = new UecOutStaffWorkListboxBean();
	/**
	 * 外包人员工作记录实体
	 */
	private UecOutStaffWork uecOutStaffWork;
	/**
	 * 外包人员工作记录查询实体
	 */
	private UecOutStaffWork qryUecOutStaffWork;

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffWorkListboxExt() {
		Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_WORK_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_WORK, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_WORK_RESPONSE);
		
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		 this.setButtonValid(false, false,false,false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecOutStaffWorkList() throws Exception {
		if (this.bean.getUecOutStaffWorkListbox().getSelectedCount() > 0) {
			uecOutStaffWork = (UecOutStaffWork) bean.getUecOutStaffWorkListbox().getSelectedItem()
					.getValue();
			if (StrUtil.isEmpty(uecOutStaffWork.getPkPsnMsg())) {
				this.setButtonValid(true, true, true, true);
			}else {
				this.setButtonValid(false, false, false, true);
			}
		}
	}

	
	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
    public void onUecOutStaffWorkQueryResponse(ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (null != uecOutStaffInfo) {
            // 判断是否为集团数据
            // 设置操作按钮，非盟市主管不可操作按钮
            if (PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, false, false, false);
            } else {
                this.setButtonValid(true, true, true, true);
            }
            qryUecOutStaffWork = new UecOutStaffWork();
            if (!StrUtil.isEmpty(uecOutStaffInfo.getPkPsnMsg())) {
                qryUecOutStaffWork.setPkPsnMsg(uecOutStaffInfo.getPkPsnMsg());
            }
            qryUecOutStaffWork.setOutStaffId(uecOutStaffInfo.getOutStaffId());
            this.onQueryUecOutStaffWorkQuery();
        } else {
            clearVariable();
            uecOutStaffInfo = null;
            ListboxUtils.clearListbox(this.bean.getUecOutStaffWorkListbox());
            this.bean.getUecOutStaffWorkListboxPaging().setTotalSize(1);
        }
    }
	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffWorkQuery() {
		clearVariable();
		UecOutStaffWork objUecOutStaffWork = UecOutStaffWork.newInstance();
		PubUtil.fillPoFromBean(bean, objUecOutStaffWork);
		qryUecOutStaffWork = objUecOutStaffWork;

		this.bean.getUecOutStaffWorkListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffWorkList();
		
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecOutStaffWork() {
		ListboxUtils.clearListbox(this.bean.getUecOutStaffWorkListbox());
		this.bean.getUecOutStaffWorkListboxPaging().setTotalSize(1);
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecOutWorkResponse(ForwardEvent event){
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffWorkListbox());
		this.bean.getUecOutStaffWorkListboxPaging().setTotalSize(1);
		
		this.setButtonValid(false, false, false, false);
	}
	
	public void onCleanUecOutStaffWorkResponse() {
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffWorkListbox());
		this.bean.getUecOutStaffWorkListboxPaging().setTotalSize(1);
		
		this.setButtonValid(false, false, false, false);
	}
	
	

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canAdd,boolean canEdit, boolean candel, boolean canView) {
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
		this.bean.getViewButton().setDisabled(!canView);
	}


	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecOutStaffWork() throws Exception {
		openUecOutStaffWorkEditWin("add");
		this.onQueryUecOutStaffWorkQuery();
	}
	/**
	 * 修改按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onEditUecOutStaffWork() throws Exception {
		if (this.uecOutStaffWork != null) {
			openUecOutStaffWorkEditWin("edit");
		} else{
			ZkUtil.showError("请选择你要修改的记录", "提示信息");
		}
		
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onViewUecOutStaffWork() throws Exception {
		if (this.uecOutStaffWork != null) {
			openUecOutStaffWorkEditWin("view");
		} else{
			ZkUtil.showError("请选择你要查看的记录", "提示信息");
		}
		
	}
	/**
	 * openUacStaffInfoWin
	 * 
	 * @param opType
	 */
	private void openUecOutStaffWorkEditWin(String opType) {
		if (null != uecOutStaffInfo) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", opType);
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			map.put("uecOutStaffWork", uecOutStaffWork);
			if ("edit".equals(opType)) {
				map.put("uecOutStaffWorkId", uecOutStaffWork.getId());
			}
			
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_WORK_EDIT_ZUL, this, map);
			try {
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecOutStaffWork = (UecOutStaffWork) event.getData();
							onQueryUecOutStaffWorkList();
						}
					}
				});
			} catch (SuspendNotAllowedException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffWork() {
		if (this.uecOutStaffWork != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecOutStaffWork.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecOutStaffWorkListbox(),
								uecOutStaffWork, "del");
						uecOutStaffWork = null;
						setButtonValid(true,false, false, false);
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutStaffWorkListboxPaging() {
		this.onQueryUecOutStaffWorkList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOutStaffWork = null;
		qryUecOutStaffWork = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffWorkList() {
		if (null != uecOutStaffInfo) {
			PageInfo pageInfo = uecOutStaffManager.findUecOutStaffWork(
					uecOutStaffInfo, this.bean.getUecOutStaffWorkListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecOutStaffWorkListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecOutStaffWorkListbox().setModel(list);
			this.bean.getUecOutStaffWorkListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
			
		}
	}
}
